﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VersionComparer.UI
{
    public partial class ItemsListControlDropdownForm : Form
    {

        UserControl ownerControl;
        Form ownerForm;
        public ItemsListControlDropdownForm()
        {
            InitializeComponent();

            MouseClick += form_MouseClick;
            KeyPress += form_KeyPress;
            KeyPreview = true;
            MaximizeBox = false;
            MinimizeBox = false;
            FormBorderStyle = FormBorderStyle.SizableToolWindow;
            SizeGripStyle = SizeGripStyle.Show;
            ShowInTaskbar = false;
            ShowIcon = false;
            ControlBox = false;
            FormBorderStyle = FormBorderStyle.None;

            var control = new ItemsListRichTextBox();
            control.EmptyMessage += "\r\nPress Ctrl + Enter to accept, or esc to cancel";
            control.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            control.Dock = DockStyle.Fill;
            Controls.Add(control);
            
        }
        public ItemsListControlDropdownForm(UserControl owner):this()
        {
            this.ownerForm = owner.FindForm();

            ownerControl = owner;
            
            this.Owner = ownerForm;
            
            ownerForm.Move += owner_Move;
            owner.Resize += owner_Resize;
            StartPosition = FormStartPosition.Manual;
            Reposition();
        }

        void form_MouseClick(object sender, MouseEventArgs e)
        {

        }

        void form_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                Close();
                return;
            }
            if (((e.KeyChar == '\r' || e.KeyChar == '\n') && ((System.Windows.Forms.Button.ModifierKeys & Keys.Control) > 0)))
            {
                Close();
                return;
            }
        }


        void owner_Resize(object sender, EventArgs e)
        {
            Reposition();
        }
        void Reposition()
        {
            Width = ownerControl.Width-25;
            //Height = Owner.Height - ownerControl.;
            Location = ownerControl.PointToScreen(new Point(0, ownerControl.Height-6));
            if (Owner == null)
            {
            }
            else
                Height = Owner.ClientRectangle.Height - (Location.Y - Owner.Location.Y);
        }
        void owner_Move(object sender, EventArgs e)
        {
            Reposition();
            
        }
        private void ItemsListControlDropdownForm_Load(object sender, EventArgs e)
        {

        }
    }
}
